import UIKit

public class CHProgressBarView2: UIView {
    
    // MARK: - 属性
    private let progressLayer = CALayer()
    private let backgroundMask = CAShapeLayer()
    private var progress: CGFloat = 0.0
    private var progressColor: UIColor
    private var backgroundColorValue: UIColor
    
    // MARK: - 初始化方法
    public  init(frame: CGRect, progressColor: UIColor, backgroundColor: UIColor = UIColor.gray) {
        self.progressColor = progressColor
        self.backgroundColorValue = backgroundColor
        super.init(frame: frame)
        setupProgressBar()
    }
    
    required init?(coder: NSCoder) {
        self.progressColor = .systemBlue
        self.backgroundColorValue = UIColor.gray
        super.init(coder: coder)
        setupProgressBar()
    }
    
    // MARK: - 设置进度条
    private func setupProgressBar() {
        // 设置背景颜色
        self.backgroundColor = backgroundColorValue
        
        // 添加进度层
        layer.addSublayer(progressLayer)
        
        // 设置圆角遮罩
        backgroundMask.path = UIBezierPath(roundedRect: bounds, cornerRadius: bounds.height / 2).cgPath
        layer.mask = backgroundMask
        
        // 初始进度为0
        setProgress(0.0, animated: false)
    }
    
    // MARK: - 布局更新
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        // 更新遮罩路径
        backgroundMask.path = UIBezierPath(roundedRect: bounds, cornerRadius: bounds.height / 2).cgPath
        layer.mask = backgroundMask
        
        // 更新进度层
        updateProgressLayer()
    }
    
    // MARK: - 更新进度层
    private func updateProgressLayer() {
        let progressWidth = bounds.width * progress
        progressLayer.frame = CGRect(
            x: 0,
            y: 0,
            width: progressWidth,
            height: bounds.height
        )
        progressLayer.backgroundColor = progressColor.cgColor
        progressLayer.cornerRadius = bounds.height / 2
    }
    
    // MARK: - 设置进度
    public  func setProgress(_ value: CGFloat, animated: Bool) {
        // 确保进度值在0到1之间
        let clampedValue = max(0, min(1, value))
        
        if animated {
            // 动画方式更新进度
            UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
                self.progress = clampedValue
                self.updateProgressLayer()
            })
        } else {
            // 直接更新进度
            progress = clampedValue
            updateProgressLayer()
        }
    }
    
    // MARK: - 更新进度颜色
    func updateProgressColor(_ color: UIColor) {
        progressColor = color
        updateProgressLayer()
    }
    
    // MARK: - 更新背景颜色
    func updateBackgroundColor(_ color: UIColor) {
        backgroundColorValue = color
        self.backgroundColor = color
    }
    
    // MARK: - 获取当前进度
    func getCurrentProgress() -> CGFloat {
        return progress
    }
    
    // MARK: - 重置进度
    func resetProgress(animated: Bool = false) {
        setProgress(0.0, animated: animated)
    }
}
